"""
 * @file main.py
 * @author SDK_TEAM
 * @brief
 * @version 0.1
 * @date 2024-05-09
 * Copyright:
 * © 2018 北京灵汐科技有限公司 版权所有。
 * 注意：以下内容均为北京灵汐科技有限公司原创，未经本公司允许，不得转载，否则将视为侵权；对于不遵守此声明或者其他违法使用以下内容者，本公司依法保留追究权。
 * © 2018 Lynxi Technologies Co., Ltd. All rights reserved.
 * NOTICE: All information contained here is, and remains the property of Lynxi. This file can not
 * be copied or distributed without the permission of Lynxi Technologies Co., Ltd.
"""

import sys

sys.path.append("../../../")

from car_plate_recognition import CarPlateRecognition
from common.python.callback_data_struct import *
from common.python.infer_process import *
from ctypes import *

import pylynchipsdk as sdk
import common.python.common as common
import argparse
import os
import signal
import multiprocessing
import signal


# Create a shared boolean value to stop running thread
cancel_flag = multiprocessing.Value("b", False)


def main(args, video_frame) -> None:
    worker = CarPlateRecognition()
    worker.input_file = args.input_path
    if args.show_type == 1:
        worker.output_path = common.append_text_to_filename(
            common.change_file_extension(args.input_path, ".264"),
            f"_device_{args.device_id}_channel_{args.channel_id}",
        )
    if args.show_type != 0 and args.show_type != 1:
        print("supported show type: 0 or 1, got args.show_type")
        exit(-1)
    if os.path.exists(worker.output_path):
        os.remove(worker.output_path)
    worker.device_id = args.device_id
    worker.channel_id = args.channel_id
    worker.plugin_path = args.plugin_path
    worker.osd_plugin = args.osd_plugin
    worker.plate_detect_model_path = args.model_path
    worker.plate_recognize_model_path = args.another_model_path
    worker.show_type = args.show_type
    worker.video_frame = video_frame
    if not worker.input_file:
        raise ValueError("input file miss!!!")
    if worker.show_type == 1 and not worker.output_path:
        raise ValueError("unspecified output path!!!")
    worker.init()
    worker.run(cancel_flag)
    worker.close()


def cancel_process(signum, frame):
    global cancel_flag
    cancel_flag.value = True


if __name__ == "__main__":
    argparser = argparse.ArgumentParser(
        description="python3 ./main.py"
    )
    argparser.add_argument("-m1", "--model_path", type=str, help="model file path")
    argparser.add_argument(
        "-m2", "--another_model_path", type=str, help="model file path"
    )
    argparser.add_argument("-i", "--input_path", help="input video path or stream url")
    argparser.add_argument("-p", "--plugin_path", type=str, help="post plugin path")
    argparser.add_argument("-o", "--osd_plugin", type=str, help="osd plugin path")
    argparser.add_argument(
        "-d",
        "--device_id",
        type=str,
        default="0",
        help='Comma-separated list of device ids, e.g., "0,1"',
    )
    argparser.add_argument("-s", "--show_type", type=int, default=0, help="show type")
    argparser.add_argument(
        "-c", "--channel_num", type=int, default=1, help="channel_num"
    )
    argparser.add_argument(
        "-j", "--config_file", type=str, default=None, help="config path"
    )

    signal.signal(signal.SIGINT, cancel_process)
    args = argparser.parse_args()
    channel_infos = get_channel_infos(args)
    start_process(main, channel_infos, cancel_flag)
